<?php
include_once(realpath(dirname(__FILE__)).'/Dao.php');
include_once(realpath(dirname(__FILE__)).'/../game.php');

class GameDao extends Dao {
    public function __construct() {
        parent::__construct();
    }

    /* inserts a game (input = Game class)
     * returns: on success: 1
     *          on error: 0
     */
    public function insert($game) {
        $gid = $game->getId(); //populated by DB
	$tm1 = $game->getTeam1Id();
	$tm2 = $game->getTeam2Id();
	$day = $game->getGameDate();
	$time = $game->getTime();
	$sql = <<<SQL
INSERT INTO game VALUES
($gid,$tm1,$tm2,
SQL;
    }

    public function remove($game) {
        die("Not yet implemented");
    }

    public function update($game) {
        die("Not yet implemented");
    }

    /* look up the schedule for a particular month
     * inputs:  $month -- integer in the range (1,12)
     * returns: array of Game objects, indexed by day of month subtract 1,
     *          e.g.: element 0 is first day of the month
     */
    public function lookupByMonth($month) {
        $sql = <<<SQL
SELECT demo from DEMO WHERE id=0
SQL;
        $result = mysql_query($sql, $this->m_cx);
	if( !$result ) { die("lookupByMonth: failed at query."); }
	$r = mysql_fetch_assoc($result);
	if( !$r ) { die("lookupByMonth: failed at fetch."); }

        if( $r['demo'] == 0 ) {
	   return $this->_emptyData($month);
	}
	else {
	   return $this->_filledData($month);
	}
    }

    /********************/
    /* Helper functions */
    /* (used for demo)  */
    /********************/

    private function _emptyData($month) {
        $r = array();
	for( $ix = 0; $ix < 31; $ix++ ) {
	    $r[$ix] = 0;
	}
	return $r;
    }

    private function _filledData($month) {
        switch( $month ) {
	    case 9:
	        $ret = array( 
		    5 =>  new Game(0, 1000, 1001, "Basketball", "PAC Gym 1", "7:00PM - 8:00 PM"),
		    12 => new Game(0, 1001, 1003, "Basketball", "PAC Gym 1", "7:00PM - 8:00 PM"),
		    19 => new Game(0, 1004, 1005, "Basketball", "PAC Gym 1", "7:00PM - 8:00 PM"),
		    26 => new Game(0, 1000, 1002, "Basketball", "PAC Gym 1", "7:00PM - 8:00 PM"),

		    8 =>  new Game(0, 1002, 1004, "Basketball", "PAC Gym 1", "8:00PM - 9:00 PM"), 
		    15 => new Game(0, 1000, 1004, "Basketball", "PAC Gym 1", "8:00PM - 9:00 PM"),
		    22 => new Game(0, 1002, 1001, "Basketball", "PAC Gym 1", "8:00PM - 9:00 PM"),
		    29 => new Game(0, 1001, 1005, "Basketball", "PAC Gym 1", "8:00PM - 9:00 PM"),

		    7 =>  new Game(0, 1003, 1005, "Basketball", "PAC Gym 2", "9:00PM - 10:00 PM"),
		    14 => new Game(0, 1002, 1005, "Basketball", "PAC Gym 2", "9:00PM - 10:00 PM"),
		    21 => new Game(0, 1001, 1004, "Basketball", "PAC Gym 2", "9:00PM - 10:00 PM"),
		    28 => new Game(0, 1004, 1003, "Basketball", "PAC Gym 2", "9:00PM - 10:00 PM")
		);
		return $ret;
		break;
	    case 10:
	        $ret = array(
		    3 => new Game(0, 1004, 1003, "Basketball", "PAC Gym 1", "7:00PM - 8:00 PM"),
		    6 => new Game(0, 1004, 1003, "Basketball", "PAC Gym 1", "8:00PM - 9:00 PM"),
		    5 => new Game(0, 1004, 1003, "Basketball", "PAC Gym 2", "9:00PM - 10:00 PM")
		);
		return $ret;
		break;
	    default: 
	        return _emptyData(1);
	}
    }

} //class
?>
